//
//  SNArtelCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/16.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNArtelCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
@interface SNArtelCell()
@property (weak, nonatomic) UIView *bgView;
@property (weak, nonatomic) UILabel *nameLabel;
@property (weak, nonatomic) UILabel *phoneLabel;
@property (weak, nonatomic) UILabel *shortnameLabel;
/** imageView */
@property (weak, nonatomic) UIImageView * iconImageView;
//@property (weak, nonatomic) UILabel *otherLabel;
/** View */
@property (weak, nonatomic) UILabel * typeNameLabel;

@end
@implementation SNArtelCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addViews];
        [self setupUI];
        self.contentView.backgroundColor = HexRGB(0xf5f5f5);
    }
    return self;
}
- (void)configWithModel:(SNOrganizationCustinfolist *)model shortname:(NSString *)shortname
{
    _model = model;
    _SHORTNAME = shortname;
    
    NSLog(@"model.CUSTNAME = %@",model.custName);
    self.nameLabel.text = [model.custName stringByReplacingOccurrencesOfString:@" " withString:@""];;
    
    CGSize nameLabelTextSize = [self foo:self.nameLabel];
    
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
       //TODO: iOS 10 字体适配....
        make.width.mas_equalTo(nameLabelTextSize.width+1);
    }];
    //TODO: 也不知道 图片是以什么形式来的, 得看服务器怎么改了, 如果是NSData 用AFN, 如果是正常就用 SDWebImage
//    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:kUserIconDefaultImage];
    [SNUtils getImageWithURLString:model.photo imageView:self.iconImageView];
    
    
    
    self.phoneLabel.text = [NSString stringWithFormat:@"电话: %@",model.phone];
    
    self.shortnameLabel.text = [shortname isEqualToString:@""]? @"未知":shortname;
}

- (void)addViews
{
    
    UIView * bgView = ({
        UIView * view = [[UIView alloc] init];
        
        view.backgroundColor = HexRGB(0xffffff);
        
        view.layer.cornerRadius = 4;
        // self.bgView.layer.masksToBounds = YES;
        
        view.layer.shadowColor = HexRGB(0xdddddd).CGColor;//shadowColor阴影颜色
        view.layer.shadowOffset = CGSizeMake(1,1);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
        view.layer.shadowOpacity = 1.0;//阴影透明度，默认0
        view.layer.shadowRadius = 4;//阴影半径，默认3
        
        [self.contentView addSubview:view];

        view;
    });
    self.bgView = bgView;
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.image = kUserIconDefaultImage;
        imageView.layer.cornerRadius = 25;
        imageView.layer.masksToBounds = YES;
        [self.contentView addSubview:imageView];
        imageView;
    });
    self.iconImageView = iconImageView;
    
    UILabel * nameLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
//        label.opaque = YES;
//        label.backgroundColor = [UIColor whiteColor];
//        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"李四李四";
        label.font = [UIFont systemFontOfSize:13];
        label.adjustsFontForContentSizeCategory = YES;
        [self.contentView addSubview:label];
        
        label;
    });
    self.nameLabel = nameLabel;
    
//    UILabel * otherLabel = ({
//        UILabel * label = [[UILabel alloc] init];
//        /** GPU 优化 */
//        //        label.opaque = YES;
//        //        label.backgroundColor = [UIColor whiteColor];
//        //        label.layer.masksToBounds = YES;
//        //------------------------------
//        label.numberOfLines = 0;
//        label.textColor = [UIColor blackColor];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.text = @"(123456)";
//        label.font = [UIFont systemFontOfSize:10];
//        
//        [self.contentView addSubview:label];
//        
//        label;
//    });
//    self.otherLabel = otherLabel;
    
    UILabel * phoneLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"188234112980";
        label.font = [UIFont systemFontOfSize:13];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.phoneLabel = phoneLabel;
    
    UILabel * typeNameLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"小农";
        label.font = [UIFont systemFontOfSize:13];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.typeNameLabel = typeNameLabel;
    
    UILabel * shortnameLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = HexRGB(0x818181);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"罗庄合作社";
        label.font = [UIFont systemFontOfSize:13];
        
        [self.contentView addSubview:label];
        
        label;
    });
    self.shortnameLabel = shortnameLabel;
}
- (CGSize)foo:(UILabel *)label
{
    CGSize size = [[[NSAttributedString alloc] initWithString:label.text attributes:@{NSFontAttributeName : label.font}] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    return size;
}
- (void)setupUI
{
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(24);
        // make.top.mas_equalTo(self.contentView.mas_top).offset(19);
        make.centerY.mas_equalTo(self.contentView.mas_centerY).offset(0);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];

    CGSize nameLabelTextSize = [self foo:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(40);
        make.top.mas_equalTo(self.iconImageView.mas_top).offset(0);
        make.width.mas_equalTo(nameLabelTextSize.width);
        //        make.width.mas_equalTo(nameLabelTextSize.width);
    }];
    
//    [self.otherLabel mas_makeConstraints:^(MASConstraintMaker * make) {
//        make.left.mas_equalTo(self.nameLabel.mas_right).offset(4);
//        make.centerY.mas_equalTo(self.nameLabel.mas_centerY).offset(0);
//    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(15);
        make.top.mas_equalTo(self.nameLabel.mas_top).offset(0);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-24);
    }];
    
    [self.typeNameLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.nameLabel.mas_left).offset(0);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10);
    }];
    
    [self.shortnameLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.nameLabel.mas_left).offset(0);
//        make.top.mas_equalTo(self.typeNameLabel.mas_bottom).offset(9);
        make.top.mas_equalTo(self.typeNameLabel.mas_bottom).offset(0);
        make.bottom.mas_lessThanOrEqualTo(self.contentView.mas_bottom).offset(-19).priorityLow();
        make.height.mas_equalTo(0);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(12);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-12);
        make.top.mas_equalTo(self.contentView.mas_top).offset(0);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
    }];
}
@end
